package com.topmap.service.impl;

import com.topmap.dao.UserDOMapper;
import com.topmap.dataobject.UserDO;
import com.topmap.error.BusinessException;
import com.topmap.error.EmBusinessError;
import com.topmap.service.UserService;
import com.topmap.service.model.UserModel;
import com.topmap.validator.ValidationResult;
import com.topmap.validator.ValidatorImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: MaHuadong
 * @Date: 2019/7/17 12:40
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDOMapper userDOMapper;


    //校验
    @Autowired
    private ValidatorImpl validator;



    @Override
    public UserModel getUserByTel(String telephone) {

        UserDO userDO=userDOMapper.selectByTelphone(telephone);


        if (userDO==null){
            return null;
        }

        //最后通过convertFromDataObject方法把userDO和userPasswordDO转换为usermodel并且返回给controller层
        return convertFromDataObject(userDO);

    }




    //用户注册具体逻辑操作
    @Override
    //支持声明式事务管理，特别是基于注解的声明式事务管理，简单易用
    @Transactional
    public void register(UserModel userModel) throws BusinessException {
        if (userModel==null){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }

        //注解版校验方法
        ValidationResult result= validator.validator(userModel);
        if (result.isHasError()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrorMsg());
        }


        //在这里我们要实现model转换成do的方法,因为我们dao层的mapper识别的是dataobject（do）文件
        UserDO userDO=convertFromUserModel(userModel);
        //把信息插入数据库

        try {
            userDOMapper.insertSelective(userDO);   //这个就没有问题
        }catch (DuplicateKeyException ex){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"手机号重复注册！");
        }
        userModel.setId(userDO.getId());

        return;


    }


    //用户登录入参校验
    @Override
    public UserModel validateLogin(String telephone, String encrptPassword) throws BusinessException {
        //通过用户的手机获取用户信息sql
        UserDO userDO= userDOMapper.selectByTelphone(telephone);
        //判空处理
        if (userDO==null){
            throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        }

        //通过userdo拿到usermodel
        UserModel userModel=convertFromDataObject(userDO);
        //对比用户信息内加密的密码是否和传输进来的密码想匹配
        if (!StringUtils.equals(encrptPassword,userModel.getPassword())){
            throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        }

        return userModel;

    }




    //返回值是UserModel,通过dataobject组装成一个model
    private UserModel convertFromDataObject(UserDO userDO) {
        //首先处理空值问题
        if (userDO==null){
            return null;
        }
        //实例化Usermodel
        UserModel userModel=new UserModel();
        //简单的使用copyProperties把对应userDO的属性copy到usermodel类中
        BeanUtils.copyProperties(userDO,userModel);

        //最后返回出去userModel
        return  userModel;
    }


    //在这里我们要实现model转换成do的方法,因为我们dao层的mapper识别的是dataobject（do）文件
    //参数是UserModel，返回值是UserDO
    private UserDO convertFromUserModel(UserModel userModel) {

        //非空判断
        if (userModel==null){
            return  null;
        }
        UserDO userDO=new UserDO();
        //  简单的使用copyProperties把对应usermodel的属性copy到userDO类中
        BeanUtils.copyProperties(userModel,userDO);

        return userDO;
    }
}
